Skip to content

Add Concurrent Merge Sort Implementation - #7544

Merged
DenizAltunkapan merged 8 commits into
TheAlgorithms:masterfrom
anshullakra007:feat/concurrent-merge-sort
Jul 31, 2026
Merged

Add Concurrent Merge Sort Implementation#7544
DenizAltunkapan merged 8 commits into
TheAlgorithms:masterfrom
anshullakra007:feat/concurrent-merge-sort

Conversation

@anshullakra007

Copy link
Copy Markdown
Contributor

Add Concurrent Merge Sort Implementation

Description

This PR introduces a ConcurrentMergeSort algorithm that accelerates the standard divide-and-conquer Merge Sort by distributing sub-array sorting tasks across multiple threads utilizing a ThreadPoolExecutor.

To prevent the overhead of thread creation and context switching from ruining overall performance, this implementation incorporates a sequential fallback threshold (set to 8,192 elements). Once a sub-array's size drops below this threshold—or when a safe maximum concurrency depth is reached—the algorithm smartly switches to a standard sequential Merge Sort. This design prevents thread starvation deadlocks and ensures optimal CPU resource utilization.

Time & Space Complexity

  • Time Complexity: $\mathcal{O}(N \log N)$ - The array is consistently divided in half and merged linearly. The concurrent threads distribute this workload without altering the fundamental algorithmic complexity.
  • Space Complexity: $\mathcal{O}(N)$ - A temporary array of size $N$ is allocated once for the merging phases, alongside $\mathcal{O}(\log N)$ auxiliary stack space utilized by the recursive calls.

Testing

Comprehensive and strictly deterministic JUnit 5 tests have been added in ConcurrentMergeSortTest.java to guarantee reliability:

  • Pre-sorted data: Verified correct handling on both strictly ascending and strictly descending arrays.
  • Identical elements: Ensured correct sorting and stability for arrays filled with identical values.
  • Edge cases: Successfully tested against empty ([]) and single-element ([42]) arrays.
  • Large datasets: Executed a load test with 100,000 randomly generated elements (using a fixed Random(42) seed for CI determinism) to intentionally exceed the 8,192 sequential threshold, verifying the multithreaded pathways against Java's heavily optimized built-in Arrays.sort().

Checklist

  • Code is formatted according to the repository's standard style guidelines.
  • No wildcard imports are used; all dependencies are explicitly imported.
  • Proper Javadocs are included for classes and public methods, detailing complexities.
  • JUnit 5 tests are exhaustive, deterministic, and pass successfully.
  • The executor thread pool cleanly shuts down via a finally block, preventing CI/CD pipeline hangs.

@codecov-commenter

codecov-commenter commented Jul 26, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.42%. Comparing base (f0b7778) to head (c7a0556).

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #7544      +/-   ##
============================================
+ Coverage     80.40%   80.42%   +0.02%     
- Complexity     7444     7454      +10     
============================================
  Files           814      815       +1     
  Lines         24016    24053      +37     
  Branches       4727     4731       +4     
============================================
+ Hits          19309    19345      +36     
+ Misses         3945     3944       -1     
- Partials        762      764       +2     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@anshullakra007
anshullakra007 force-pushed the feat/concurrent-merge-sort branch from 10d265c to fc8ab59 Compare July 26, 2026 16:47
@anshullakra007

Copy link
Copy Markdown
Contributor Author

Hi maintainers! 👋

Just a friendly follow-up on this PR. All CI checks are passing, and I've addressed the previous feedback. Whenever someone has time, I'd really appreciate a review.

Thank you for your time!

@DenizAltunkapan DenizAltunkapan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@anshullakra007 thank you for the contribution

@DenizAltunkapan
DenizAltunkapan enabled auto-merge (squash) July 31, 2026 11:11
@DenizAltunkapan
DenizAltunkapan merged commit 1f4e2f8 into TheAlgorithms:master Jul 31, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants